/**
 * Created by Administrator on 2017/9/22.
 */


$(document).ready(function(){
    //字符串转对象方法
    //phoneNumber=123346&pwd=rwwa&code=1415 -->{"phoneNumber":123346,"pwd":rwwa,"code":1415}
    function tranObj(str){
        var obj = {};
        if(str.indexOf("&")!=-1){
            //有多条数据
            var arr1 = str.split("&");
            for(var i=0; i< arr1.length; i++){
                var arr2 = arr1[i].split("=");
                obj[arr2[0]] = arr2[1];
            }
        }else{
            //单条数据
            var arr1 = str.split("=");
            obj[arr1[0]] = arr1[1];
        }
        return obj;
    }


    var rqs = {
        "url":"../hz_1706/spUser/version1/login.php",
       // "data":{"phonenumber":"17855832113","pwd":"123456"},
        "dataType":"json",
        "type":"post",
        "success":function(res){
            // console.log(res);
            if(res.status==1){
                //登陆成功     判断是否勾选7天免登陆
                // window.history.go(-1);
                if($("div.remember input[type=checkbox]")[0].checked){
                    console.log($("div.remember input[type=checkbox]")[0].checked);
                    //记住密码 存7天
                    //判断是否从注册页面跳转过来
                    if($.cookie("spRegist")){
                        //注册页面跳转过来
                        $.removeCookie("spRegist");
                        $.cookie("spInfor",JSON.stringify({"username":$("input[name=phonenumber]").val()}),{expires:7});
                        window.location.href = "index.html";
                    }else{
                        $.cookie("spInfor",JSON.stringify({"username":$("input[name=phonenumber]").val()}),{expires:7});
                        window.history.go(-1);
                        console.log(JSON.parse($.cookie("spInfor")));
                    }

                }else{
                    console.log($("div.remember input[type=checkbox]")[0].checked);
                    if($.cookie("spRegist")){
                        //注册页面跳转过来
                        $.removeCookie("spRegist");
                        $.cookie("spInfor",JSON.stringify({"username":$("input[name=phonenumber]").val()}));
                        window.location.href = "index.html";
                    }else{
                        //未勾选
                        $.cookie("spInfor",JSON.stringify({"username":$("input[name=phonenumber]").val()}));
                       window.history.go(-1);
                    }

                }

            }else{
                alert("用户名或密码错误");
            }


        }
    };
    $("input[name=phonenumber]")[0].reg = /^(13|15|17|18|14)[0-9]{9}$/;
    $("input[name=pwd]")[0].reg = /^.{6,16}/;
    $(".item").not(".confirm").not(".remember").children("input").on("blur",function(){
        this.status = this.reg.test($(this).val());
        if(!this.status){
            $(this).parent().children("p").remove();
            var oP = document.createElement("p")
            $(this).parent().append(oP);
            // console.log(this.getAttribute("exten"));
            oP.innerText = this.getAttribute("exten")+"格式错误"
        }else{
            $(this).parent().children("p").remove();
        }
    })


    // var regtry = /^(?![^a-zA-Z]+$)(?!\D+$).{8,15}$/;
    // console.log(regPhone.test(1785583211));
    // console.log(regPwd.test("12345"));
    // $.ajax(rqs);

    $("#loginBtn").on("click",function(){
        var numSta = 0;
        this.status = false;
        $(".item").not(".confirm").not(".remember").children("input").each(function(index,ele){
            if(ele.status){
                numSta++;
            }
        })
        if(numSta==2){
            //当所有正则判断都成功的时候，就让次按钮状态为true
            this.status = true;
        }
        if(this.status){
            rqs.data = tranObj($("form").serialize());
            $.ajax(rqs);
        }


    })



})